package com.wannengbang.cloudleader.service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.PlanDetailsListBean;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 信用卡列表
 */
public class PlanDetailsListAdapter extends RecyclerView.Adapter<PlanDetailsListAdapter.ViewHolder> {

    private List<PlanDetailsListBean.DataBean.DetailBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public void setOnCallBackListener(PlanDetailsListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    public PlanDetailsListAdapter(List<PlanDetailsListBean.DataBean.DetailBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_plan_details_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlanDetailsListBean.DataBean.DetailBean dataBean = listsBeans.get(position);
        holder.tvId.setText(dataBean.getDate());
        String repayment_amount = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getRepayment_amount()) / 100);
        String consume_amount = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getConsume_amount()) / 100);
        holder.tvRepaymentAmount.setText(repayment_amount);
        holder.tvConsumeAmount.setText(consume_amount);
        PlanDetailsDailyListAdapter listAdapter = new PlanDetailsDailyListAdapter(dataBean.getDaily() == null ? new ArrayList<>() : dataBean.getDaily());
        holder.recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(context));
        holder.recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((status, id) -> {
            onCallBackListener.onCallBack(1, status, id);
        });
        holder.tvCommit.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, dataBean.getRepayment_status(), dataBean.getId());
        });

        //   "status": 1,            // 1待完成 2已完成
        if (dataBean.getRepayment_status() == 1) {

            holder.iv_plan_complete.setVisibility(View.GONE);
            holder.tvCommit.setVisibility(View.VISIBLE);
//            holder.tvCommit.setBackgroundResource(R.drawable.bg_plan_add);
//            holder.tvCommit.setText("完成还款");
//            holder.tvCommit.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.iv_plan_complete.setVisibility(View.VISIBLE);
            holder.tvCommit.setVisibility(View.GONE);
//            holder.tvCommit.setBackgroundResource(R.drawable.shape_rectangle_conner_gary5);
//            holder.tvCommit.setText("已完成");
//            holder.tvCommit.setTextColor(context.getResources().getColor(R.color.gary));
        }
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_id)
        TextView tvId;
        @BindView(R.id.iv_plan_complete)
        ImageView iv_plan_complete;
        @BindView(R.id.tv_commit)
        TextView tvCommit;
        @BindView(R.id.tv_repayment_amount)
        TextView tvRepaymentAmount;
        @BindView(R.id.tv_consume_amount)
        TextView tvConsumeAmount;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int status, int childId);
    }
}
